﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace RuleCode.Common
{
    public static class ConvertValue
    {
        public static object ConvertTo(string text, System.Type toType, ref bool result)
        {
            if (toType != null)
            {
                if (toType.IsSubclassOf(typeof(TableFieldType)))
                {
                    TableFieldType field = Activator.CreateInstance(toType) as TableFieldType;
                    try
                    {
                        field.StringValue = text;
                        return field.ObjectValue;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(int))
                {
                    try
                    {
                        int field = Convert.ToInt32(text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(string))
                {
                    return text;
                }
                else if (toType == typeof(Int64))
                {
                    try
                    {
                        Int64 field = Convert.ToInt64(text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(double))
                {
                    try
                    {
                        double field = Convert.ToDouble(text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(float ))
                {
                    try
                    {
                        float  field = Convert.ToSingle (text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(decimal ))
                {
                    try
                    {
                        decimal field = Convert.ToDecimal(text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(DateTime))
                {
                    try
                    {
                        DateTime  field = Convert.ToDateTime(text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(bool))
                {
                    try
                    {
                        bool  field = Convert.ToBoolean (text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(byte ))
                {
                    try
                    {
                        byte field = Convert.ToByte (text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }
                else if (toType == typeof(char ))
                {
                    try
                    {
                        char field = Convert.ToChar(text);
                        return field;
                    }
                    catch
                    {
                        result = true;
                    }
                }

            }
            return null;
        }
    }
}
